package com.example.peibei.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.Order;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDFragment;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.MySendBillPresenter;
import com.example.peibei.ui.activity.OrderDetailActivity;
import com.example.peibei.ui.adapter.MyBillListAdapter;
import com.example.peibei.ui.adapter.UserBillListAdapter;
import com.example.peibei.ui.weight.EmptyRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MySendBillFragment extends WDFragment {
    @BindView(R.id.order_list)
    EmptyRecyclerView order_list;
    @BindView(R.id.empty_view)
    LinearLayout empty_view;

    private String mid;
    List<Order.RecordsDTO> orders;
    private UserBillListAdapter homeOrderListAdapter;
    private MySendBillPresenter mySendBillPresenter;
    private SPUtils spUtils;
    private String token;

    public MySendBillFragment(String mid) {
        this.mid = mid;
    }

    @Override
    public String getPageName() {
        return "我发的单fragment";
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_dynamic_list;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(getActivity(), SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        orders = new ArrayList<>();
        order_list.setLayoutManager(new LinearLayoutManager(getActivity()));
        homeOrderListAdapter = new UserBillListAdapter(orders);
        order_list.setAdapter(homeOrderListAdapter);
        order_list.setEmptyView(empty_view);
        mySendBillPresenter = new MySendBillPresenter(new OrderCall());
        mySendBillPresenter.reqeust(true,mid,"Bearer " + token);
        homeOrderListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Order.RecordsDTO order = (Order.RecordsDTO) adapter.getData().get(position);
                String publishOrderId = order.getPublishOrderId();
                Bundle bundle = new Bundle();
                bundle.putString("publishOrderId",publishOrderId);
                intent(OrderDetailActivity.class,bundle);
            }
        });
    }

    class OrderCall implements DataCall<Order> {
        @Override
        public void success(Order data, Object... args) {
            //添加列表并刷新
            if (mySendBillPresenter.getPage() == 1) {
                orders.clear();
            }
            homeOrderListAdapter.setNewInstance(data.getRecords());
            homeOrderListAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }
}
